package com.lenin.omena.client;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.maps.client.MapPane;
import com.google.gwt.maps.client.MapPaneType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.MarkerPanel.State;
import com.lenin.omena.shared.domain.User;

public class CustomMarker extends Overlay {

	private final LatLng latLng;

    private final MarkerPanel markerPanel;

    private MapWidget parentMap;
    
    private AbsolutePanel parentPanel;
   
    private MapPane pane;
    
    private User user;

    /**
     * Main constructor
     * @param latLng
     */
    public CustomMarker(AbsolutePanel parentPanel, User user, LatLng latLng) {
        /* Save our inputs to the object */
        this.latLng = latLng;
        this.user = user;
        this.parentPanel = parentPanel;
        
        /* Create the panel to hold the text */
        markerPanel = new MarkerPanel(parentPanel, user);
       
        /* Panel gets added to the map and placed in the initialize method */
    }

    @Override
    protected final void initialize(MapWidget map) {
        /* Save a handle to the parent map widget */
        parentMap = map;  //If we need to do redraws we'll need this
       
        /* Add our textPanel to the main map pane */
        pane = map.getPane(MapPaneType.MARKER_PANE);
        pane.add(markerPanel);
       
        markerPanel.initialize(pane);
        redraw(true);
    
    }
    

    @Override
    protected final Overlay copy() {
        return new CustomMarker(parentPanel, user, getLatLng());
    }
   
    @Override
    protected final void redraw(boolean force) {
    	
    	/* Place the textPanel on the pane in the correct spot */
        Point locationPoint = parentMap.convertLatLngToDivPixel(getLatLng());
        markerPanel.setLocation(locationPoint);
    
    }

    @Override
    protected final void remove() {
        markerPanel.removeFromParent();
    }

    public LatLng getLatLng() {
        return latLng;
    }

    
    public void toggle() {
    	
    	if(markerPanel.getState() == State.OPEN) {
    		markerPanel.shrink();
    		markerPanel.getElement().getStyle().setZIndex(3000);
    	} else {
    		markerPanel.expand();
    		markerPanel.getElement().getStyle().setZIndex(3005);
    	}
    	
    }
    
    
    public State getState() {
    	return markerPanel.getState();
    }
    
    public void addClickHandler(MouseUpHandler handler) {
    	markerPanel.addMouseUpHandler(handler);
    }
	
}
